﻿using System.Text;
using msilgc.cbuilder.CSyntaxTree;

namespace msilgc.CSyntaxTree {
    internal class BooleanBranchStatement : ICStatement {
        public string TargetLabel { get; set; }
        public IVariable Condition { get; set; }
        public bool TrueNonZero { get; set; }

        public BooleanBranchStatement(string label, IVariable cond, bool trueNonZero) {
            TargetLabel = label;
            Condition = cond;
            TrueNonZero = trueNonZero;
        }

        public void Emit(StringBuilder builder) {
            builder.Append("if(");
            Condition.Emit(builder);
            if(TrueNonZero)
            {
                builder.Append(" != 0) { goto ");
            }
            else
            {
                builder.Append(" == 0) { goto ");
            }
            builder.Append(TargetLabel);
            builder.AppendLine("; }");
        }
    }
}
